
//安卓版本高于Android 9
if (device.sdkInt > 28) {
    //等待截屏权限申请并同意
    threads.start(function () {
        packageName('com.android.systemui').text('立即开始').waitFor();
        text('立即开始').click();
    });
}
//申请截屏权限
if (!requestScreenCapture(true)) {
    toast("请求截图失败");
    exit()
}

const el = {
    _findMultiColors() {
        var img = captureScreen();
        let a = arguments

        return images.findMultiColors(
            img,
            a[0],
            a[1],
            a[2]
        )
    },
    isMain() {
        // 主界面
        let isMain = el._findMultiColors(
            "#de020c", [[451, 12, "#4897e9"], [347, 1427, "#facd00"], [288, 1387, "#fce305"], [455, 1521, "#0297ed"]], { region: [70, 0, 555, 1599], threshold: [26] }
        )

        return isMain
    },
    startBtn() {
        return el._findMultiColors(
            "#ffa21e",[[17,5,"#000000"],[32,7,"#ffffff"],[168,7,"#ffffff"]],{region:[301,1175,395,168],threshold:[26]}
        )
    },
    /** 匹配界面 */
    isMate() {
        return el._findMultiColors(
            "#000000",[[18,5,"#17e6fe"],[40,0,"#000000"],[64,0,"#17e6fe"],[86,-3,"#000000"]],{region:[330,706,186,108],threshold:[26]}
        )
    },
    /** 游戏中 */
    isPlaying() {
        return el._findMultiColors(
            "#4a26ff",[[14,4,"#ffffff"],[54,2,"#efedf8"],[64,2,"#4a26ff"],[122,12,"#4a26ff"]],{region:[3,0,222,98],threshold:[26]}
        )
    },
    /** 游戏结束 */
    playEnd() {
        return el._findMultiColors(
            "#ffffff",[[14,-3,"#000000"],[19,-1,"#ffffff"],[37,2,"#ffffff"],[56,0,"#ffffff"],[56,14,"#000000"]],{region:[300,1385,311,141],threshold:[26]}
        )
    },

    /** 游戏结束-开始转盘 */
    playEndStart() {
        return (
        el._findMultiColors(
           "#ffffff",[[-432,898,"#00cdfe"],[-429,898,"#000001"],[-419,899,"#ffffff"],[-389,905,"#000000"],[-385,910,"#00abd4"],[-374,908,"#ffffff"],[-358,905,"#000000"],[-330,922,"#001116"],[-321,925,"#00cdfe"]],{region:[373,1372,147,71],threshold:[26]}
        )
            || el._findMultiColors(
            "#000000",[[12,5,"#ffffff"],[23,13,"#00a0c6"],[43,32,"#00cdfe"],[58,36,"#ffffff"],[97,41,"#000000"]],{region:[344,1362,203,95],threshold:[26]}
        ) || el._findMultiColors(
            "#00cdfe",[[31,1,"#000203"],[56,4,"#ffffff"],[83,1,"#000000"],[124,-4,"#ffffff"]],{region:[303,1346,303,130],threshold:[26]}
        ))
    },

    /** 游戏结束-领取 */
    playEndGet() {
        return (
        el._findMultiColors(
            "#000000",[[7,1,"#ffffff"],[14,1,"#000000"],[23,3,"#ffffff"],[27,0,"#000000"],[34,2,"#ffffff"],[46,5,"#00cdfe"],[70,7,"#00cdfe"],[90,2,"#00cdfe"],[78,3,"#ffffff"]],{region:[349,1405,196,94],threshold:[26]}
        )
        || el._findMultiColors(
            "#66e50e",[[6,1,"#181818"],[11,1,"#ffffff"],[16,3,"#ffffff"],[20,3,"#000000"],[24,3,"#ffffff"],[53,3,"#66e50e"]],{region:[313,1353,277,115],threshold:[26]}
        ) || el._findMultiColors(
            "#b7ff2d",[[92,3,"#000000"],[105,4,"#ffffff"],[135,5,"#000000"],[139,2,"#66e50e"],[171,2,"#ffffff"],[173,-4,"#020600"]],{region:[479,1353,283,116],threshold:[26]}
        ))
    },

    /** 看广告 */
    playAD() {
        return el._findMultiColors(
            "#ecc408",[[9,0,"#000000"],[16,-1,"#ffffff"],[25,-2,"#000000"],[32,-3,"#ffffff"],[41,-2,"#2f2702"],[47,1,"#ecc408"],[74,17,"#000000"],[91,16,"#ecc408"]],{region:[29,955,284,112],threshold:[26]}
        )
    },

    /** 关闭广告 */
    closeAD() {
        return (
            el._findMultiColors(
                "#faf8fa",[[5,-4,"#202124"],[15,-8,"#fafafa"],[18,-3,"#202124"],[24,-1,"#fafafa"],[12,12,"#faf8fa"]],{region:[26,28,57,54],threshold:[26]}
            ) ||
            el._findMultiColors(
            "#fafafa",[[10,-5,"#222126"],[29,-4,"#202124"],[10,11,"#c4c4c5"],[26,12,"#202124"],[32,5,"#fafafa"],[8,4,"#fafafa"],[17,10,"#222126"]],{region:[814,24,67,62],threshold:[26]}
        ))
    },

    /** 领取奖励 */
    getReward() {
        return el._findMultiColors(
            "#00cdfe",[[4,2,"#000405"],[9,4,"#ffffff"],[30,16,"#000000"],[35,16,"#ffffff"],[48,25,"#008aaa"],[63,36,"#000000"],[69,32,"#f6f6f6"]],{region:[335,1490,242,95],threshold:[26]}
        )
    },
}

function random(min, max) {
    return Math.round(Math.random() * (max - min)) + min;
}

function start() {
    while (true) {
        // 开始游戏
        let p = el.startBtn()

        if(p) {
            click(p.x, p.y)
            sleep(2000)
        }

        // 是否需要看广告
        p = el.playAD()

        if(p) {
            click(p.x, p.y)
            sleep(2000)

            // 等待关闭按钮出现
            do {
                p = el.closeAD()

                if(p) {
                    click(p.x, p.y)
                    sleep(2000)
                }

            } while (!p);
        }

        p = el.getReward()

        if(p) {
            click(p.x, p.y)
            sleep(2000)
            click(830,487)
            sleep(2000)
        }

        // 游戏匹配界面
        do {
            p = el.isMate()

            if(p) {
                sleep(1000)
                console.log('游戏匹配中');
            }
        } while (p);

        // 游戏中
        do {
            p = el.isPlaying()

            if(p) {
                sleep(1)
                console.log('游戏中');
                
                for (let idx = 0; idx < 300; idx++) {
                    let x = random(38, 864)
                    let y = random(372, 1328)

                    click(x, y)
                    sleep(1)
                }
            }
        } while (p);

        // 游戏结束-开始转盘
        p = el.playEndStart()
        if(p) {
            click(p.x, p.y)
            sleep(7000)
        }

        // 游戏结束-转盘领取
        p = el.playEndGet()
        if(p) {
            click(p.x, p.y)
            sleep(2000)
        }

        // 游戏结束-点击确认
        p = el.playEnd()
        if(p) {
            click(p.x, p.y)
            sleep(2000)
        }
    }
}

start()


